<template>
  <div class="app-container">
    <div style="margin-bottom: 20px;">
      <el-input v-model="listQuery.keyword" placeholder="请输入活动名称" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.status" placeholder="请选择活动状态" clearable class="filter-item" style="width: 180px">
        <el-option label="未开始" value="1" />
        <el-option label="进行中" value="2" />
        <el-option label="已结束" value="3" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 20px;" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-plus" style="float:right" @click="handleAdd">
        添加活动
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      :element-loading-text="g.loadingStr"
      :header-cell-style="{background:g.mainColor}"
    >
      <el-table-column label="活动类型" align="center" width="160" fixed>
        <template>
          <span>拼团活动</span>
        </template>
      </el-table-column>
      <el-table-column label="活动名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动时间" align="center">
        <template slot-scope="{row}">
          <span>
            开始：{{ row.beginTime | parseTime('{y}-{m}-{d} {h}:{i}') }}
            <br> 结束：{{ row.endTime | parseTime('{y}-{m}-{d} {h}:{i}') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center">
        <template slot-scope="{row}">
          <span v-if="row.failureTime === null">
            {{ row.effectiveTime === null ? '' : row.effectiveTime/1000/60/60/24 + '天' }}
          </span>
          <span v-else>
            {{ row.failureTime | parseTime('{y}-{m}-{d} {h}:{i}') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="优惠券" align="center">
        <template slot-scope="{row,$index}">
          <span style="color:blue;" @click="handleInfo(row,$index)">查看</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="90px">
        <template slot-scope="{row}">
          <span>{{ row.status | activityStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="240px">
        <template slot-scope="{row,$index}">
          <el-button type="success" size="mini" @click="handleDetail(row,$index)">
            详情
          </el-button>
          <el-button type="primary" size="mini" @click="handleStatus(row,$index)">
            统计
          </el-button>
          <el-button v-if="row.status === 1" type="primary" size="mini" @click="handleUpdate(row,$index)">
            上架
          </el-button>
          <el-button v-if="row.status === 2" type="primary" size="mini" @click="handleUpdate(row,$index)">
            下架
          </el-button>
          <el-button v-if="row.status === 3" type="danger" size="mini" @click="handleDel(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog
      :title="infoTitle"
      :visible.sync="dialogVisible"
      width="600px"
    >
      <div v-show="fullDiscount.length !== 0">
        <label style="display:block;padding-left:23px;font-size:16px;margin-bottom: 10px;">满减券</label>
        <label class="info-label" style="height: 0px; margin-bottom: 30px;">优惠券金额（元）</label>
        <label class="info-label" style="height: 0px; margin-bottom: 30px;">满XXX减XXX（元）</label><br>
        <div v-for="(item, index) in fullDiscount" :key="index">
          <label class="info-label" style="">{{ item.couponMoney }}</label>
          <label class="info-label" style="">{{ item.money }}</label><p />
        </div>
      </div>

      <div v-show="discountCoupon.length !== 0" style="margin-top:20px;">
        <label style="display:block;padding-left:23px;font-size:16px;margin-bottom: 10px;">立减券</label>
        <label class="info-label" style="height: 0px; margin-bottom: 30px;">优惠券金额（元）</label><br>
        <div v-for="(item, index) in discountCoupon" :key="index">
          <label class="info-label" style="">{{ item.money }}</label><p />
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { provinceAndCityData, CodeToText } from 'element-china-area-data'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { activityList, activityEdit, activityDetail } from '@/api/activity'

export default {
  name: 'ActivityList',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        keyword: '',
        status: null,
        isDel: 0
      },
      areaOptions: provinceAndCityData,

      dialogVisible: false,
      infoTitle: '',
      discountCoupon: [],
      fullDiscount: []
    }
  },
  created() {
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      if (this.listQuery.showRange === '1') {
        this.listQuery.provinces = null
        this.listQuery.citys = null
      }
      activityList(this.listQuery).then(response => {
        console.log(response.data)
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.getList()
    },
    areaChange(value) {
      const provinces = CodeToText[value[0]]
      const city = CodeToText[value[1]]
      this.listQuery.provinces = provinces
      this.listQuery.citys = city
    },
    handleInfo(row) {
      console.log(row)
      this.dialogVisible = true
      this.infoTitle = row['name']
      this.refreshInfo(row['id'])
    },
    refreshInfo(id) {
      activityDetail({ id }).then(response => {
        const data = response.data
        this.discountCoupon = []
        this.fullDiscount = []
        data.platformVolumes.forEach(element => {
          if (element['type'] === 1) {
            // eslint-disable-next-line no-useless-call
            this.discountCoupon.push.call(this.discountCoupon, { money: element['amount'] })
          }
          if (element['type'] === 2) {
            // eslint-disable-next-line no-useless-call
            this.fullDiscount.push.call(this.fullDiscount, { couponMoney: element['amount'], money: element['fullReduction'] })
          }
        })
      })
    },
    handleDetail(row) {
      console.log(row)
      this.$router.push({ name: 'ActivityEdit', params: { id: row.id }})
    },
    handleStatus(row, index) {

    },
    handleUpdate(row, index) {
      console.log(row)
      this.$confirm('是否确认' + (row['status'] === 1 ? '上架' : '下架') + '该活动？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(JSON.stringify(this.multipleSelection))
        activityEdit({
          id: row['id'],
          status: row['status'] === 1 ? 2 : 1
        }).then(response => {
          this.$message({ message: '操作成功', type: 'success' })
          this.list[index]['status'] = row['status'] === 1 ? 2 : 1
        })
      })
    },
    handleDel(row, index) {
      console.log(row)

      this.$confirm('是否确认删除该活动？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(JSON.stringify(this.multipleSelection))
        activityEdit({
          id: row['id'],
          isDel: 1
        }).then(response => {
          this.$message({ message: '操作成功', type: 'success' })
          this.list.splice(index, 1)
        })
      })
    },
    handleAdd() {
      this.$router.push('ActivityAdd')
    }
  }
}
</script>
<style lang="scss" scoped>
.info-label {
	font-weight: 400;
  width: 160px;
  display:inline-block;
  text-align:center;
  height: 20px;
  line-height: 20px;
}
</style>
